/*
 * Main.java
 *
 * Created on 20 marzec 2007, 01:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package client;

import data.Employee;
import data.Team;
import data.VacQuota;
import data.Vacation;
import java.awt.Component;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import server.VacationServer;
import server.Constants;
import util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author xavex
 */
public class VacationClient
{
    private VacationServer vs;
    private static VacationClient instance;
    
    
    
    private VacationClient()
    {
        initializeRMI();
    }
    

    private void initializeRMI()
    {        
        // set the security manager for the client
        System.setSecurityManager(new RMISecurityManager());
        

        //get the remote object from the registry
        String url = "rmi://" + Constants.SERVER_ADDRESS + "/" + Constants.SERVER;
        
        try
        {   
            vs = (VacationServer)Naming.lookup(url);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        };     
    }
    

    
    public VacationServer getRemoteServer()
    {
        return vs;
    }       
            
    public static VacationClient getInstance()
    {
        if( instance == null )
        {
            instance = new VacationClient();
        }
        
        return instance;
    }            
            
    public static void main(String[]  args)
    {
        //rmiTest();
        
        ( new MainFrame() ).setVisible( true );
                
//        hibernateTest();        
//        listDatabase();
                
   }
    
    public static void printException( Exception e, Component parent )
    {
        JOptionPane.showMessageDialog( parent, e.toString(), "Error", JOptionPane.ERROR_MESSAGE );
    }
    
    
    private static void listDatabase() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        
        List<Vacation> list = session.createQuery( "From Vacation" ).list();
        
        for( Vacation vac : list ) {
            for( Employee emp : vac.getEmployees() ) {
                System.out.println( emp.getTeam().getName() );
            }                
        }
        
        
        session.getTransaction().commit();       
    }
    
    private static void rmiTest() {
        // set the security manager for the client
        System.setSecurityManager(new RMISecurityManager());
        //get the remote object from the registry
        
        System.out.println("dupa1");
        try
        {
            System.out.println("Security Manager loaded");
            String url = "//" + Constants.SERVER_ADDRESS + "/" + Constants.SERVER;
            VacationServer remoteObject = (VacationServer)Naming.lookup(url);
            System.out.println("Got remote object");
            System.out.println("connected with vacation server: " + remoteObject.getHelloWorld() );

          
            Set<Team> teams = remoteObject.getTeams();           
            
            System.out.println("Number of teams: " + teams.size() + "\r\n");
            
//            Team lastTeam = null;
            
            for( Team t : teams ) {
                System.out.println( t.getName() );
//                lastTeam = t;
            }

            Team newTeam = new Team();
            newTeam.setName( "nowy team" );
            
            Employee emp = new Employee();
            emp.setName( "dupa" );
            
//            Employee emp2 = new Employee();
//            emp2.setName( "kuper" );
            
//            emp2.setTeam( lastTeam );
//            lastTeam.getMembers().add( emp2 );
            
            emp.setTeam( newTeam );
            newTeam.getMembers().add( emp );
            
            teams.add( newTeam );
           
            remoteObject.saveTeams( teams );
        }
        catch (RemoteException exc) {
            System.out.println("Error in lookup: " + exc.toString()); 
        }
        catch (java.net.MalformedURLException exc) {
            System.out.println("Malformed URL: " + exc.toString());   
        }
        catch (java.rmi.NotBoundException exc)  {
            System.out.println("NotBound: " + exc.toString());
        }
        catch( Exception exc ) {
            System.out.println( exc );
        }
       
    }

    private static void hibernateTest() {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        
        Vacation vac = new Vacation();
        
        vac.setVacationDate( new Date() );
        
//        session.save( vac );
                
        Employee emp = new Employee();
        
        emp.setName( "dupa wolowa" );
        emp.setLocked( false );
                
//        session.save( emp );
        
        emp.getVacations().add( vac );
        
//        session.save( emp );
        
        Team team = new Team();
        team.setName( "dupy wolowe" );        
//        session.save( team );
        
        team.getMembers().add( emp );
//        session.save( team );
  
//        session.save( vac );
//        session.save( emp );
        
        VacQuota vacQuota = new VacQuota();
        
        vacQuota.setFromDate( new Date() );
        vacQuota.setToDate( new Date() );
        vacQuota.setQuota( 5 );
        
        
        team.getQuotas().add( vacQuota );
        
        emp.setTeam( team );
        vacQuota.setTeam( team );
        
        session.save( team );
        session.getTransaction().commit();
    }
}
